import os
import sys
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.httpclient
import nest_asyncio

pro_path = os.path.dirname(os.path.realpath(__file__))
root_path = os.path.dirname(os.path.dirname(pro_path))
sys.path.append(pro_path)
sys.path.append(root_path)

from bot.enterprise_wechat_gpt.config.conf import *
from bot.enterprise_wechat_gpt.request_handler import query_handler, transfer_handler, revoke_handler, redis_handler, active_send_handler

if __name__ == '__main__':
    app = tornado.web.Application(handlers=[
        (r"/response", query_handler.QueryHandler),
        (r"/set_transfer", transfer_handler.TransferHandler),
        (r"/revoke_msg", revoke_handler.RevokeHandler),
        (r"/redis/(.*)", redis_handler.RedisHandler),
        (r"/response_active", active_send_handler.ActiveSendHandler),
    ],
        debug=False
    )
    http_server = tornado.httpserver.HTTPServer(app)
    port = profile_['server']['port']
    http_server.bind(port)
    http_server.start(profile_['server']['process_num'])
    logging.info(f"********** server started on port:{port} **********")
    # 为了解决执行异步请求大模型的时候抛RuntimeError('This event loop is already running')
    # 且只能放在这个位置
    nest_asyncio.apply()
    tornado.ioloop.IOLoop.current().start()
